package com.jinwei.qunguanjia.web.user.controller;

import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.PageDataUtil;
import com.jinwei.qunguanjia.common.utils.PageUtils;
import com.jinwei.qunguanjia.common.utils.StringUtils;
import com.jinwei.qunguanjia.user.dto.UserDTO;
import com.jinwei.qunguanjia.user.service.UserService;
import com.jinwei.qunguanjia.web.user.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 群管理相关接口
 *
 * @author yuanzs
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 获取群分页列表
     * @param userVO
     */
    @GetMapping("/listPage")
    public PageDataInfo<UserVO> list(UserVO userVO) throws IllegalAccessException {
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userVO, userDTO);
        if (!StringUtils.isEmpty(userVO.getPhoneNumber())) {
            userDTO.setPhoneNumber("like:"+userVO.getPhoneNumber());
        }
        if (!StringUtils.isEmpty(userVO.getRealName())) {
            userDTO.setRealName("like:"+userVO.getRealName());
        }
        PageUtils.startPage();
        PageDataUtil<UserDTO, UserVO> pageDataUtil = new PageDataUtil<>();
        return pageDataUtil.transform(userService.listPage(userDTO), UserVO.class);
    }

    /**
     * 获取群详细信息
     * @param userId 群id
     * @return 群详细信息
     */
    @GetMapping(value = "/{userId}")
    public Response<UserVO> getInfo(@PathVariable String userId) {
        UserDTO userDTO = userService.get(userId);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userDTO, userVO);
        return Response.success(userVO);
    }
    
}
